package activity

import (
	"lao-a/helper"
	"lao-a/models/model"
	"strings"
	"time"

	"github.com/yl10/kit/orm"
)

const (
	ACTIVITY_READY = iota
	ACTIVITY_NORMAL
	ACTIVITY_CANCEL
	ACTIVITY_EXPIRE
)

// DAO 当前数据库操作对象
type DAO struct {
	ctx *helper.Context
	db  *orm.XormPlus
}

// NewDAO 初始化DAO
func NewDAO(ctx *helper.Context) *DAO {
	return &DAO{
		ctx: ctx,
		db:  ctx.GetDB(),
	}
}

// SaveActivity 保存活动
func (m *DAO) SaveActivity(act *model.TaActivity) (*model.TaActivity, error) {
	user := m.ctx.User.(*model.SysUser)
	now := time.Now()

	act.CreateDate = now
	act.CreateUser = user.UserId

	if _, err := m.db.Insert(act); err != nil {
		return nil, helper.LogError("保存活动失败", err)
	}

	return act, nil
}

// UpdateActivity 更新活动
func (m *DAO) UpdateActivity(act *model.TaActivity, cols []string) error {
	if _, err := m.db.Update(act, cols, "activity_id=?", act.ActivityId); err != nil {
		return helper.LogError("更新活动失败", err)
	}

	return nil
}

// UpdateActivityStatus 更新活动状态
func (m *DAO) UpdateActivityStatus(act *model.TaActivity) error {
	if _, err := m.db.Update(act, []string{"status"}, "activity_id=?", act.ActivityId); err != nil {
		return helper.LogError("更新活动状态失败", err)
	}

	return nil
}

// GetActivityList 获取活动列表
type ActivityExtra struct {
	model.TaActivity `xorm:"extends"`
	UserName         string
}

func (m *DAO) GetActivityList(where, limit []string) ([]ActivityExtra, error) {
	var acts []ActivityExtra

	sqlSelect := `
		SELECT
			t.*, s.user_name
		FROM
			ta_activity t
		JOIN sys_user s ON t.create_user = s.user_id
	`
	sqlOrderBy := " order by create_date desc"
	sqlLimit := " limit " + limit[0] + "," + limit[1]
	sqlWhere := ""
	if where != nil {
		sqlWhere = " where " + strings.Join(where, " and ")
	}

	sql := sqlSelect + sqlWhere + sqlOrderBy + sqlLimit

	if err := m.db.SQL(sql).Find(&acts); err != nil {
		return nil, helper.LogError("获取活动列表失败", err)
	}

	return acts, nil
}

// GetActivityById 获取活动详情失败
func (m *DAO) GetActivityById(actId int) (*model.TaActivity, error) {
	var act model.TaActivity

	if _, err := m.db.Where("activity_id=?", actId).Get(&act); err != nil {
		return nil, helper.LogError("获取活动详情失败", err)
	}

	return &act, nil
}

// GetLimitActivitiesPerType 每种分类获取限定的活动
func (m *DAO) GetLimitActivitiesPerType(limit int) ([]model.TaActivity, error) {
	query := `
		SELECT
			*
		FROM
			ta_activity t
		INNER JOIN (
			SELECT
				a.type_id,
				a.create_date
			FROM
				ta_activity a
			LEFT JOIN ta_activity b ON a.type_id = b.type_id
			AND a.status = b.status
			AND a.to_index = b.to_index
			AND a.create_date <= b.create_date
			WHERE
				a.status = ?
			AND
				a.to_index = ?
			GROUP BY
				a.type_id,
				a.create_date
			HAVING count(b.create_date) < ?
		) s ON t.type_id = s.type_id
		AND t.create_date = s.create_date
		WHERE
			t.status = ?
		AND
			t.to_index = ?
		ORDER BY
			t.type_id,
			t.create_date DESC
	`
	var yes = 1
	var acts []model.TaActivity
	if err := m.db.SQL(query, ACTIVITY_NORMAL, yes, limit, ACTIVITY_NORMAL, yes).
		Find(&acts); err != nil {
		return nil, helper.LogError("按分类获取活动信息出错", err)
	}

	return acts, nil
}

// GetActivitiesInSlider 获取首页Slider推荐活动
func (m *DAO) GetActivitiesInSlider(limit int) ([]model.TaActivity, error) {
	var yes = 1
	var acts []model.TaActivity
	if err := m.db.Where("status=?", ACTIVITY_NORMAL).
		And("to_slider=?", yes).
		Desc("create_date").
		Limit(limit).
		Find(&acts); err != nil {
		return nil, helper.LogError("获取首页Slider推荐活动出错", err)
	}

	return acts, nil
}
